﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ProcessMonitor.Helpers
{
    public static class Utils
    {
        public static IDictionary<string, string> GetApplicationPoolNames()
        {
            IDictionary<string, string> dictionary = new Dictionary<string, string>();

            ProcessStartInfo psi = new ProcessStartInfo("cscript", "iisapp.vbs");
            psi.WorkingDirectory = @"c:\windows\system32";
            psi.RedirectStandardOutput = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;

            Process process = Process.Start(psi);
            process.WaitForExit(2000);
            if (process.HasExited)
            {
                while (!process.StandardOutput.EndOfStream)
                {
                    string line = process.StandardOutput.ReadLine();
                    if (!line.StartsWith("PID"))
                    {
                        continue;
                    }

                    string pid = line.Substring(17, 5).Trim();
                    string apn = line.Substring(35).Trim();
                    dictionary.Add(pid, apn);
                }
            }

            return dictionary;
        }
    }
}
